﻿using System;
using System.IO;
using System.Net;
using System.Text;

namespace ServiceHelper
{
    public class NetworkUtils
    {
        /// <summary>
        /// Get response from a request with Unicode encoding
        /// </summary>
        /// <param name="url"></param>
        /// <param name="request"></param>
        /// <param name="isPostMethod"></param>
        /// <returns></returns>
        public static HttpWebResponse GetResponse(string url, string request, bool isPostMethod)
        {
            return GetResponse(url, request, isPostMethod, Encoding.Unicode);
        }

        /// <summary>
        /// Get response from a request with encoding
        /// </summary>
        /// <param name="url"></param>
        /// <param name="request"></param>
        /// <param name="isPostMethod"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static HttpWebResponse GetResponse(string url, string request, bool isPostMethod, Encoding encoding)
        {
            try
            {
                HttpWebRequest httpWebRequest;
                HttpWebResponse httpWebResponse;

                // set method: GET or POST
                if (isPostMethod)
                {
                    // create request
                    byte[] buffer = Encoding.ASCII.GetBytes(request);
                    httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

                    httpWebRequest.Method = "POST";
                    // set length and content type
                    httpWebRequest.ContentLength = buffer.Length;
                    httpWebRequest.ContentType = "application/x-www-form-urlencoded";

                    Stream postwriter = httpWebRequest.GetRequestStream();
                    postwriter.Write(buffer, 0, buffer.Length);
                    postwriter.Close();
                }
                else
                {
                    // create request
                    string serviceUrl = url + request;
                    httpWebRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
                    httpWebRequest.Method = "GET";
                } // end if IsPostMethod

                httpWebRequest.Timeout = 300000;

                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                return httpWebResponse;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// Get a string response from HttpWebResponse
        /// </summary>
        /// <param name="httpWebResponse"></param>
        /// <returns></returns>
        public static String GetResponse(HttpWebResponse httpWebResponse)
        {
            if (httpWebResponse == null)
                return null;

            try
            {
                Stream answer = httpWebResponse.GetResponseStream();
                if (answer == null)
                    return null;
                StreamReader answerReader = new StreamReader(answer);
                string result = answerReader.ReadToEnd();
                answer.Close();
                answerReader.Close();
                httpWebResponse.Close();
                return result;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// Replace html syntax
        /// </summary>
        /// <param name="strString"></param>
        /// <returns></returns>
        public static string ReplaceSpecialCharsForXML(string strString)
        {
            string strRet = String.Empty;
            if (strString != null)
            {
                strRet = strString.Replace("\"", "&quot;");
                strRet = strRet.Replace("&", "&amp;");
                strRet = strRet.Replace("’", "&apos;");
                strRet = strRet.Replace("'", "&apos;");
                strRet = strRet.Replace("<", "&lt;");
                strRet = strRet.Replace(">", "&gt;");
            }
            return strRet;
        }

        /// <summary>
        /// Replace html syntax
        /// </summary>
        /// <param name="strString"></param>
        /// <returns></returns>
        public static string ReplaceHTMLSyntax(string strString)
        {
            string strRet = String.Empty;
            if (strString != null)
            {
                strRet = strString.Replace("&quot;", "\"");
                strRet = strRet.Replace("&amp;", "&");
                strRet = strRet.Replace("&apos;", "’");
                strRet = strRet.Replace("&apos;", "'");
                strRet = strRet.Replace("&lt;", "<");
                strRet = strRet.Replace("&gt;", ">");
            }
            return strRet;
        }
    }
}